package ej1.Main;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Stack;

public class MutationPath {

	private Stack<Strain> _path;
	
	public MutationPath(){
		
		_path = new Stack<Strain>();
		
	}
	
	public MutationPath(Stack<Strain> aPath) {
		_path = aPath;
	}

	public boolean isLongerThan(MutationPath anotherMutationPath) {
		
		return anotherMutationPath.isShorterThanMutationPath(this);
	}

	protected boolean isShorterThanMutationPath(MutationPath anotherMutationPath) {
		
		return length() < anotherMutationPath.length();
	}

	protected int length() {
		
		return _path.size();
	}

	public MutationPath precededBy(Strain aStrain) {

		_path.push(aStrain);
		
		return this;
	}
	
	@SuppressWarnings("unchecked")
	public MutationPath clone(){
		return new MutationPath((Stack<Strain>)_path.clone());
	}
	
	public void saveAs(String aFilePath) {
		
		FileWriter writer;
		try {
			writer = new FileWriter(aFilePath);
			BufferedWriter bufferedWriter = new BufferedWriter(writer);
			
			String solutionLine = "";
			while (!_path.empty())
				solutionLine += _path.pop().getId() + " ";
			
			solutionLine.trim();
			solutionLine += "\n";
			
			bufferedWriter.append(solutionLine);
			
			bufferedWriter.close();
			writer.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
	}

	
}
